package com.uusee.framework.bo.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.uusee.framework.bo.UniversalBo;
import com.uusee.framework.dao.UniversalDao;
import com.uusee.framework.util.page.PageHelper;
import com.uusee.framework.util.page.PageInfo;
import com.uusee.framework.util.query.CriteriaInfo;
import com.uusee.framework.util.query.FilterWrapper;
import com.uusee.framework.util.query.QueryReturnObject;
import com.uusee.framework.util.query.SortWrapper;

/**
 * 业务逻辑对象基础实现类
 * 
 * @author <a href="mailto:wangr.bj@gmail.com">WangRui</a> <a href="mailto:du_yi@bbn.cn">DuYi</a>
 */
public class UniversalBoImpl implements UniversalBo {
	protected final Log log = LogFactory.getLog(getClass());

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getById(java.lang.Class, java.io.Serializable)
	 */
	public <T> T getById(Class<T> clazz, Serializable id) {
		return universalDao.getById(clazz, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getAll(java.lang.Class)
	 */
	public <T> List<T> getAll(Class<T> clazz) {
		return universalDao.getAll(clazz);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#doDelete(java.lang.Class, java.io.Serializable)
	 */
	public <T> void doDelete(Class<T> clazz, Serializable id) {
		universalDao.delete(clazz, id);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#doSave(java.lang.Object)
	 */
	public <T> T doSave(T o) {
		return universalDao.save(o);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#doUpdate(java.lang.Object)
	 */
	public <T> void doUpdate(T o) {
		universalDao.update(o);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#doMerge(java.lang.Object)
	 */
	public <T> T doMerge(T o) {
		return universalDao.merge(o);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getCount(java.lang.Class)
	 */
	public <T> int getCount(Class<T> modelClass) {
		return universalDao.getCount(modelClass);
	}
	
	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getEntitiesByCriteriaInfo(java.lang.Class,
	 *      com.uusee.framework.util.query.CriteriaInfo)
	 */
	public <T> List<T> getEntities(Class<T> modelClass, int start,int offset) {
		CriteriaInfo ci = new CriteriaInfo();
		ci.setFirstResult(start);
		ci.setMaxResult(offset);
		ci.setOffset(true);
		return universalDao.getEntitiesByCriteriaInfo(modelClass, ci);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getQueryReturnObject(java.lang.Class, int, int)
	 */
	public <T> QueryReturnObject<T> getQueryReturnObject(Class<T> modelClass, int pageNo, int offset) {
		CriteriaInfo ci = new CriteriaInfo();
		ci.setPageNo(pageNo);
		ci.setMaxResult(offset);
		ci.setOffset(true);

		QueryReturnObject<T> qro = getQueryReturnObjectByCriteriaInfo(modelClass,ci);
		return qro;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getCountByCriteriaInfo(java.lang.Class,
	 *      com.uusee.framework.util.query.CriteriaInfo)
	 */
	public <T> int getCountByCriteriaInfo(Class<T> modelClass, CriteriaInfo ci) {
		return universalDao.getCountByCriteriaInfo(modelClass, ci);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getEntitiesByCriteriaInfo(java.lang.Class,
	 *      com.uusee.framework.util.query.CriteriaInfo)
	 */
	public <T> List<T> getEntitiesByCriteriaInfo(Class<T> modelClass, CriteriaInfo ci) {
		return universalDao.getEntitiesByCriteriaInfo(modelClass, ci);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.uusee.framework.bo.UniversalBo#getQueryReturnObjectByCriteriaInfo(java.lang.Class,
	 *      com.uusee.framework.util.query.CriteriaInfo)
	 */
	public <T> QueryReturnObject<T> getQueryReturnObjectByCriteriaInfo(Class<T> modelClass, CriteriaInfo ci) {
		QueryReturnObject<T> qro = new QueryReturnObject<T>();
		PageInfo pageInfo = null;
		if (ci.isOffset()) {
			int totalResult = universalDao.getCountByCriteriaInfo(modelClass, ci);
			if (totalResult == 0) {
				qro.setEmptyReturn(true);
				return qro;
			} else {
				qro.setEmptyReturn(false);
			}
			pageInfo = PageHelper.fillPageInfo(ci.getPageNo(), ci.getMaxResult(), totalResult);
			ci.setFirstResult(pageInfo.getFirstResult());

			qro.setPageInfo(pageInfo);
			qro.setResults(universalDao.getEntitiesByCriteriaInfo(modelClass, ci));
			return qro;
		} else {
			qro.setResults(universalDao.getEntitiesByCriteriaInfo(modelClass, ci));
			return qro;
		}
	}

	// ====================== IoC ============================
	protected UniversalDao universalDao;

	public void setUniversalDao(UniversalDao universalDao) {
		this.universalDao = universalDao;
	}
}
